package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.TargetingSpecTemplateRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.TargetingSpecTemplate;

import java.util.List;

/**
 * Created by Administrator on 2015/3/25.
 */
@Service
public class TargetingSpecTemplateRdbServiceImpl extends DaoTemplate implements TargetingSpecTemplateRdbService {



    @Override
    public List<TargetingSpecTemplate> findByUserId(String userId) {
        String sql = "select * from targeting_spec_template where user_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{userId}, DefaultRowMapper.autoRowMapper(TargetingSpecTemplate.class));
    }


    @Override
    public Long save(final TargetingSpecTemplate template) {
        return saveResultId(template);
    }

    @Override
    public Integer update(final TargetingSpecTemplate template) {
        String sql = "UPDATE targeting_spec_template " +
                "SET user_id = ?, description = ?, name = ?, " +
                "spec = ?, type = ? , fb_connection_object_id = ? , product_configuration_id = ? ," +
                "org_id = ?, create_utc_time = ? , create_time = ? WHERE interest_id = ?";
        return this.getJdbcTemplate().update(sql, template.getUserId(), template.getDescription(),
                template.getName(), template.getSpec(), template.getType(),
                template.getFbConnectionObjectId(), template.getProductConfigurationId(),
                template.getOrgId(), template.getCreateUtcTime(), template.getCreateTime(),
                template.getInterestId());
    }


    @Override
    public Long countByOrgIdFbConnectionObjectIdProductConfigurationIdAndName(Long orgId, Long fbConnectObjectId, Long productConfigurationId, String name) {
        String sql = "SELECT COUNT(1) FROM targeting_spec_template WHERE org_id=? and fb_connection_object_id = ? and product_configuration_id = ?  and name=?";
        return getJdbcTemplate().queryForObject(sql, new Object[]{orgId, fbConnectObjectId, productConfigurationId, name}, Long.class);
    }

    @Override
    public List<TargetingSpecTemplate> findByOrgIdAndFbConnectObjectIdAndProductConfigurationId
            (Long orgId, Long fbConnectionObjectId, Long productConfigurationId) {
        String sql = "select * from targeting_spec_template where  org_id = ? and fb_connection_object_id = ? and product_configuration_id = ?";
        return getJdbcTemplate().query(sql, new Object[]{orgId, fbConnectionObjectId, productConfigurationId}, DefaultRowMapper.autoRowMapper(TargetingSpecTemplate.class));
    }
}
